import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# 加载图像
image_path = r'/root/autodl-tmp/Culane/driver_100_30frame/05251517_0433.MP4/00000.jpg'
image = Image.open(image_path)

pred_xs1 = [
    1.1909, 1.1810, 1.1728, 1.1598, 1.1485, 1.1410, 1.1273, 1.1213, 1.1101,
    1.1008, 1.0877, 1.0781, 1.0668, 1.0574, 1.0463, 1.0375, 1.0269, 1.0165,
    1.0051, 0.9952, 0.9850, 0.9748, 0.9627, 0.9545, 0.9445, 0.9362, 0.9227,
    0.9129, 0.9037, 0.8935, 0.8838, 0.8722, 0.8613, 0.8512, 0.8405, 0.8307,
    0.8218, 0.8093, 0.8007, 0.7889, 0.7820, 0.7706, 0.7587, 0.7491, 0.7395,
    0.7284, 0.7194, 0.7091, 0.6952, 0.6875, 0.6755, 0.6667, 0.6569, 0.6447,
    0.6360, 0.6244, 0.6160, 0.6054, 0.5940, 0.5825, 0.5761, 0.5633, 0.5522,
    0.5420, 0.5336, 0.5226, 0.5109, 0.5014, 0.4912, 0.4819, 0.4714, 0.4685
]
pred_ys1 = [
    1.0053, 0.9910, 0.9781, 0.9634, 0.9485, 0.9358, 0.9196, 0.9072, 0.8927,
    0.8794, 0.8646, 0.8508, 0.8363, 0.8226, 0.8079, 0.7944, 0.7803, 0.7664,
    0.7519, 0.7379, 0.7239, 0.7101, 0.6950, 0.6814, 0.6671, 0.6542, 0.6391,
    0.6249, 0.6111, 0.5970, 0.5834, 0.5691, 0.5548, 0.5408, 0.5264, 0.5123,
    0.4990, 0.4840, 0.4706, 0.4553, 0.4425, 0.4284, 0.4137, 0.3998, 0.3860,
    0.3714, 0.3578, 0.3443, 0.3288, 0.3158, 0.3008, 0.2872, 0.2736, 0.2586,
    0.2452, 0.2303, 0.2169, 0.2030, 0.1888, 0.1735, 0.1611, 0.1466, 0.1322,
    0.1177, 0.1044, 0.0904, 0.0757, 0.0618, 0.0475, 0.0338, 0.0197, 0.0000
]
pred_xs2 = [
    1.2064, 1.1968, 1.1868, 1.1747, 1.1646, 1.1551, 1.1441, 1.1361, 1.1254,
    1.1148, 1.1030, 1.0929, 1.0822, 1.0723, 1.0620, 1.0525, 1.0419, 1.0312,
    1.0204, 1.0103, 1.0001, 0.9894, 0.9787, 0.9699, 0.9602, 0.9501, 0.9381,
    0.9285, 0.9189, 0.9088, 0.8983, 0.8871, 0.8765, 0.8662, 0.8559, 0.8463,
    0.8362, 0.8251, 0.8155, 0.8053, 0.7965, 0.7851, 0.7742, 0.7645, 0.7544,
    0.7440, 0.7343, 0.7229, 0.7113, 0.7021, 0.6912, 0.6819, 0.6714, 0.6604,
    0.6507, 0.6403, 0.6309, 0.6201, 0.6089, 0.5990, 0.5901, 0.5781, 0.5675,
    0.5578, 0.5484, 0.5372, 0.5263, 0.5166, 0.5067, 0.4969, 0.4864, 0.4685
]
pred_ys2 = [
    0.9947, 0.9808, 0.9656, 0.9521, 0.9388, 0.9234, 0.9113, 0.8957, 0.8819,
    0.8670, 0.8538, 0.8393, 0.8257, 0.8112, 0.7977, 0.7831, 0.7690, 0.7548,
    0.7410, 0.7269, 0.7127, 0.6983, 0.6853, 0.6707, 0.6569, 0.6416, 0.6285,
    0.6145, 0.6002, 0.5861, 0.5715, 0.5577, 0.5438, 0.5296, 0.5158, 0.5018,
    0.4869, 0.4738, 0.4590, 0.4461, 0.4307, 0.4166, 0.4032, 0.3890, 0.3746,
    0.3610, 0.3464, 0.3317, 0.3191, 0.3040, 0.2907, 0.2762, 0.2616, 0.2484,
    0.2336, 0.2204, 0.2056, 0.1913, 0.1774, 0.1645, 0.1487, 0.1351, 0.1214,
    0.1077, 0.0928, 0.0786, 0.0651, 0.0509, 0.0370, 0.0225, 0.0085, 0.0000
]

targ_xs1 = [
    0.3324, 0.3359, 0.3393, 0.3426, 0.3460, 0.3495,
    0.3531, 0.3566, 0.3601, 0.3635, 0.3669, 0.3703,
    0.3736, 0.3770, 0.3806, 0.3842, 0.3877, 0.3911,
    0.3946, 0.3982, 0.4020, 0.4057, 0.4092, 0.4126,
    0.4161, 0.4197, 0.4234, 0.4271, 0.4308, 0.4344,
    0.4381, 0.4418, 0.4456, 0.4493, 0.4530, 0.4567,
    0.4604, 0.4642, 0.4682, 0.4721, 0.4758, 0.4795,
    0.4832, 0.4870, 0.4910, 0.4950, 0.4990, 0.5029,
    0.5067, 0.5105, 0.5142, 0.5179, 0.5218, 0.5258,
    0.5297, 0.5334, 0.5372, 0.5500
]
targ_ys1 = [
    0.9977, 0.9837, 0.9697, 0.9556, 0.9414, 0.9273, 0.9132, 0.8992, 0.8851,
    0.8710, 0.8570, 0.8429, 0.8288, 0.8146, 0.8005, 0.7865, 0.7725, 0.7583,
    0.7441, 0.7300, 0.7159, 0.7019, 0.6879, 0.6738, 0.6596, 0.6455, 0.6314,
    0.6174, 0.6033, 0.5892, 0.5751, 0.5610, 0.5469, 0.5328, 0.5188, 0.5047,
    0.4905, 0.4763, 0.4623, 0.4483, 0.4342, 0.4202, 0.4060, 0.3919, 0.3777,
    0.3636, 0.3496, 0.3356, 0.3215, 0.3075, 0.2934, 0.2792, 0.2651, 0.2510,
    0.2370, 0.2230, 0.2088, 0.2066
]
targ_xs2 = [
    0.3506, 0.3542, 0.3576, 0.3609, 0.3642, 0.3676,
    0.3712, 0.3748, 0.3783, 0.3817, 0.3851, 0.3885,
    0.3919, 0.3952, 0.3988, 0.4024, 0.4060, 0.4093,
    0.4128, 0.4164, 0.4201, 0.4238, 0.4274, 0.4309,
    0.4343, 0.4379, 0.4416, 0.4452, 0.4489, 0.4526,
    0.4562, 0.4600, 0.4637, 0.4674, 0.4711, 0.4748,
    0.4785, 0.4823, 0.4862, 0.4902, 0.4940, 0.4977,
    0.5014, 0.5051, 0.5090, 0.5130, 0.5170, 0.5210,
    0.5249, 0.5286, 0.5323, 0.5360, 0.5399, 0.5439,
    0.5478, 0.5516, 0.5553, 0.5500
]
targ_ys2 = [
    1.0023, 0.9881, 0.9740, 0.9599, 0.9459, 0.9319, 0.9178, 0.9037, 0.8895,
    0.8754, 0.8614, 0.8472, 0.8332, 0.8192, 0.8052, 0.7910, 0.7768, 0.7628,
    0.7488, 0.7348, 0.7207, 0.7065, 0.6924, 0.6783, 0.6643, 0.6503, 0.6362,
    0.6221, 0.6080, 0.5939, 0.5799, 0.5658, 0.5517, 0.5376, 0.5235, 0.5094,
    0.4954, 0.4814, 0.4673, 0.4531, 0.4390, 0.4249, 0.4109, 0.3969, 0.3828,
    0.3688, 0.3546, 0.3405, 0.3264, 0.3122, 0.2982, 0.2842, 0.2702, 0.2560,
    0.2419, 0.2278, 0.2137, 0.1878
]

print(len(pred_xs1), len(pred_ys1), len(pred_xs2), len(pred_ys2))
print(len(targ_xs1), len(targ_ys1), len(targ_xs2), len(targ_ys2))
# 将 pred_xs1 中的每个值乘以 255
def scale_coordinates(*coords, scale=255):
    return [[x * scale for x in coord] for coord in coords]

pred_xs1, pred_ys1, pred_xs2, pred_ys2, targ_xs1, targ_ys1, targ_xs2, targ_ys2 = scale_coordinates(
    pred_xs1, pred_ys1, pred_xs2, pred_ys2, targ_xs1, targ_ys1, targ_xs2, targ_ys2
)

fill_value = 255

def draw_region(img, inside_xs, inside_ys, fill_value=255):
    pt_list = list(zip(inside_xs, inside_ys))
    cv2.fillPoly(img, [np.array(pt_list, dtype=np.int32)], fill_value)
    return img

def sort_by_ys(xs, ys, reverse=False):
    combined = sorted(zip(xs, ys), key=lambda pair: pair[1], reverse=reverse)
    return zip(*combined)

# 对 pred_xs1 和 pred_ys1 排序
pred_xs1, pred_ys1 = sort_by_ys(pred_xs1, pred_ys1, reverse=True)
# 对 pred_xs2 和 pred_ys2 排序
pred_xs2, pred_ys2 = sort_by_ys(pred_xs2, pred_ys2, reverse=False)
# 对 targ_xs1 和 targ_ys1 排序
targ_xs1, targ_ys1 = sort_by_ys(targ_xs1, targ_ys1, reverse=True)
# 对 targ_xs2 和 targ_ys2 排序
targ_xs2, targ_ys2 = sort_by_ys(targ_xs2, targ_ys2, reverse=False)

img1 = np.zeros((255, 255), np.uint8)
img2 = np.zeros((255, 255), np.uint8)
img3 = np.zeros((255, 255), np.uint8)

def draw_combined_region(img, xs1, ys1, xs2, ys2, fill_value):
    xs = xs1 + xs2
    ys = ys1 + ys2
    return draw_region(img, xs, ys, fill_value)

pred_img = draw_combined_region(img1, pred_xs1, pred_ys1, pred_xs2, pred_ys2, fill_value)
targ_img = draw_combined_region(img2, targ_xs1, targ_ys1, targ_xs2, targ_ys2, fill_value)
Alls_img = draw_combined_region(img3, pred_xs1, pred_ys1, pred_xs2, pred_ys2, fill_value)
Alls_img = draw_combined_region(img3, targ_xs1, targ_ys1, targ_xs2, targ_ys2, fill_value)


area_1 = np.sum(pred_img == fill_value)
area_2 = np.sum(targ_img == fill_value)
area_com = np.sum(Alls_img == fill_value)
print("area1 ={} area2={} area3={} ".format(area_1, area_2, area_com))
iou = (area_1 + area_2 - area_com) * 1.0 / area_com
print(iou)


# 显示图像
plt.figure(figsize=(10, 10))

plt.subplot(1, 3, 1)
plt.title('Predicted Image')
plt.imshow(pred_img, cmap='gray')

plt.subplot(1, 3, 2)
plt.title('Target Image')
plt.imshow(targ_img, cmap='gray')

plt.subplot(1, 3, 3)
plt.title('All Combined Image')
plt.imshow(Alls_img, cmap='gray')

plt.show()
